package org.budo.redis.jdbc.driver.statement.hash;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.budo.jdbc.driver.resultset.impl.ObjectArrayResultSet;
import org.budo.redis.jdbc.driver.BudoRedisJdbcConnection;
import org.budo.redis.jdbc.driver.BudoRedisJdbcPreparedStatement;
import org.budo.redis.jdbc.driver.executor.RedisStatementExecutor;
import org.budo.redis.jdbc.driver.statement.AbstractRedisStatement;
import org.budo.support.lang.util.MapUtil;

/**
 * @author lmw
 */
public class HMSET extends AbstractRedisStatement {
    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException{
        byte[] key = this.parameter(0, preparedStatement);
        byte[][] hash = this.parameterArray(1, preparedStatement);
        Map map = MapUtil.newMap((Object[]) hash);

        RedisStatementExecutor statementExecutor = ((BudoRedisJdbcConnection) preparedStatement.getConnection()).getStatementExecutor();
        String hmset = statementExecutor.hmset(key, map);

        ResultSet resultSet = new ObjectArrayResultSet(new String[] { "hmset" }, new Object[] { hmset });
        ((BudoRedisJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}